import { Reducer,Effect, Subscription} from 'umi'
import {getRemoteList,editRecord,deleteRecord,addRecord} from './service'
import { message } from 'antd';
import {SingleUserType} from './data.d'

export interface UserState {
    data: SingleUserType[];
    meta: {
        total: number;
        per_page: number;
        page: number;
    };
}

interface UserModelType {
    namespace: 'users';
    state: UserState;
    reducers: {
        getList: Reducer<UserState>;
    },
    effects: {
        getRemote: Effect,
        edit:Effect,
        delete:Effect,
        add:Effect
    },
    subscriptions: {
        setup:Subscription
    }
}

const UserModel:UserModelType = {
    namespace: 'users',
    state: {
        data:[],
        meta:{
            total: 0,
            per_page: 5,
            page: 1,
        }
    },
    reducers: {
        getList(state,{payload}) {
            // console.log('reducers here')
            return payload;
        }
    },
    effects: {
        *getRemote({payload:{page,per_page}},{put,call}) : any {
            const data = yield call(getRemoteList, { page, per_page });
            if(data) {
                yield put({
                    type:'getList',
                    payload: data,
                });
            } else {
                message.error('Show failed');
            }
        },
        *edit({payload:{id,values}},{ put, call, select}) : any {
            const data = yield call(editRecord,{ id, values});
            if(data) {
                message.success('Edit successfully')
                const {page,per_page} = yield select((state:any)=> state.users.meta);
                yield put({
                    type:'getRemote',
                    payload:{
                        page,
                        per_page
                    }
                });
            } else {
                message.error('Edit failed');
            }
        },
        *add({payload:{id,values}},{ put, call,select}) : any {
            const data = yield call(addRecord,{ values});
            if(data) {
                message.success('Add successfully')
                const {page,per_page} = yield select((state:any)=> state.users.meta);
                yield put({
                    type:'getRemote',
                    payload:{
                        page,
                        per_page
                    }
                });
            } else {
                message.error('Add failed');
            }
        },
        *delete({payload:{id}},{ put, call, select}) : any {
            const data = yield call(deleteRecord,{id});
            if(data) {
                message.success('Delete successfully')
                const {page,per_page} = yield select((state:any)=> state.users.meta);
                yield put({
                    type:'getRemote',
                    payload:{
                        page,
                        per_page,
                    }
                });
            } else {
                message.error('Delete failed');
            }
        }
    },
    subscriptions: {
        setup({ dispatch,history }) {
            return history.listen(({pathname}) => {
                if(pathname ==='/users'){
                    dispatch({
                        type:'getRemote',
                        payload: {
                            page: 1,
                            per_page: 5,
                        }
                    });
                } 
            });
        },
    },
};
export default  UserModel;